import { } from '@/api/audit/index';

const state = () => {
    return {
        // 随机题列表
        list: [],
        // 当前题号
        currentPage: 0,
        // 题数
        total: 20,
        // 答题卡
        answerSheet: []
    }
}


const mutations = {
    // 获取50道试题
    SET_LIST(state, form) {
        state.list = form
    },
    // 下一题
    DOWN_QUESTION(state, data) {
        let current = data.currentPage
        if (state.currentPage >= 0) {
            if (state.answerSheet[current]) {
                state.answerSheet.splice(current, 1, data)
            } else {
                state.answerSheet.push(data)
            }
            state.currentPage++
        }
    },
    // 上一题
    UP_QUESTION(state, data) {
        let current = data.currentPage
        if (state.currentPage < state.total) {
            if (state.answerSheet[current]) {
                state.answerSheet.splice(current, 1, data)
            } else {
                state.answerSheet.push(data)
            }
            state.currentPage--
        }
    },
    SELECT_QUESTION(state, select) {
        state.currentPage = select
    } 
}

const actions = {
    // 获取数据
    async getQuestionsList({ state, commit }) {
        let { data } = await this.$axios.get("/exam/rand-list?pageSize=" + state.total)
        commit("SET_LIST", data.data)
    },
    currentState({ state, getters, commit }, { list, val }) {
        let answers = JSON.parse(getters.currentQuestion.answers).join("");
        let currentData = [...list].sort().join("");
        let trueOrFalse = currentData == answers ? true : false
        let obj = {
            currentPage: state.currentPage,
            answers: list,
            trueOrFalse,
        };
        if (val == "up") {
            commit("UP_QUESTION", obj)
        } else {
            commit("DOWN_QUESTION", obj)
        }
    },
}

const getters = {
    // 当前题
    currentQuestion(state) {
        return state.list[state.currentPage]
    }
}

export {
    state,
    mutations,
    actions,
    getters
}